package com.gitee.elf.core.entity.ai

import com.gitee.elf.core.entity.AbstractBukkitProxyEntity
import com.gitee.elf.core.entity.EntityInstance
import com.gitee.elf.core.entity.ProxyEntity
import org.bukkit.entity.Player
import org.bukkit.entity.Wolf
import taboolib.module.ai.navigationMove

class BukkitFollowAi(instance: EntityInstance) : AbstractBukkitEntityAi(instance) {

    val AbstractBukkitProxyEntity<*>.canMove: Boolean
        get() = if (entity is Wolf) {
            !(entity as Wolf).isSitting
        } else true

    override fun shouldExecute(): Boolean {
        // 如果是玩家
        if (owner is Player) {
            return owner.isOnline && !owner.isDead
        }

        return !owner.isDead && !bukkitEntity.isDead
    }


    override fun updateTask() {
        if (ownerWorld != entityWorld) {
            bukkitEntity.teleport(ownerLocation)
        }

        val distance = ownerLocation.distance(entityLocation)
        // 如果距离大于64格 直接传送
        if (distance > 32) {
            bukkitEntity.teleport(ownerLocation)
        }
        // 移动
        else if (distance > 5.0 && bukkitMapping.canMove) {
            bukkitLivingEntity!!.navigationMove(ownerLocation, bukkitMapping.moveSpeed * 5)
        }
    }

}